package com.ai.demo.controller;

import com.ai.demo.bean.ActorFilms;
import com.ai.demo.service.MultiModelService;
import com.ai.demo.service.PromptTemplatesService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
public class ChatController {
    @Autowired
    @Qualifier("deepSeekAiChatClient")
    ChatClient chatClient;


    //private final ChatClient chatClient;
    //public ChatController(ChatClient.Builder chatClientBuilder) {
    //    this.chatClient = chatClientBuilder.build();
    //}

    @Autowired
    MultiModelService multiModelService;

    @Autowired
    PromptTemplatesService promptTemplatesService;


    /**
     * @param userInput 提示词
     * @return 返回 AI 模型响应的字符串内容
     */
    @GetMapping("/ai")
    String generation(String userInput) {
        //Default System Text：
        //chatClient=chatClient.mutate().defaultSystem("你是一个只会法语的AI助手").build();
        return chatClient.prompt()
                .user(userInput)
                .call()
                .content();
    }


    @GetMapping("/ai/simple")
    public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message, String voice) {
        chatClient = chatClient.mutate().defaultSystem("You are a friendly chat bot that answers question in the voice of a {voice}")
                .build();

        return Map.of("completion",
                this.chatClient.prompt()
                        .system(sp -> sp.param("voice", voice))
                        .user(message)
                        .call()
                        .content());
    }


    /**
     * 下面显示了一个返回包含元数据的ChatResponse对象的示例，该示例在call（）方法之后调用chatResponse（）。
     *
     * @param userInput 提示词
     * @return 返回一个 ChatResponse 对象，包含响应的详细信息，例如生成的内容（可能有多个生成结果）、使用的令牌数（token count）等
     */
    @GetMapping("/ai/ChatResponse ")
    ChatResponse generationChatResponse(String userInput) {
        return chatClient.prompt()
                .user(userInput)
                .call()
                .chatResponse();
    }

    @GetMapping("/ai/stream")
    Flux<String> generationStream(String userInput) {
        Flux<String> responseStream = this.chatClient.prompt()
                .user(userInput)
                .stream()
                .content();

        //responseStream.subscribe(
        //        System.out::println, // 处理每个生成的字符串
        //        error -> System.err.println("Error: " + error.getMessage()), // 处理错误
        //        () -> System.out.println("Stream completed") // 处理流结束
        //);
        return responseStream;
    }

    /**
     * @param userInput 提示词
     * @return 将 AI 模型的响应内容转换为指定的 Java 类型。
     */
    @GetMapping("/ai/entity")
    ActorFilms generationEntity(String userInput) {
        return chatClient.prompt()
                .user(userInput)
                .call()
                .entity(ActorFilms.class);
    }


    @GetMapping("/deepseek")
    String generationDeepSeek(String userInput) {
        return this.multiModelService.multiClientFlow(userInput);
    }

    /**
     * 提示模板
     *
     * @param composer
     * @return
     */
    @GetMapping("/moviesByComposer")
    public String getMoviesByComposer(String composer) {
        return promptTemplatesService.getMoviesByComposer(composer);
    }
}
